﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApp1.Util
{
    public enum LogType
    {
        All,
        Information,
        Debug,
        Success,
        Failure,
        Warning,
        Error,
        Data,
    }


    public class LogHelper
    {
		private static object logLock;

		private static LogHelper _instance;

        public string LogPath = "Log";

		private LogHelper() { }

        public static LogHelper Instance
		{
			get 
			{
                if (_instance == null)
                {
                    _instance = new LogHelper();
					logLock = new object();
                }
                return _instance;
			}
		}

		public void WriteLog(string logContent, LogType logType = LogType.Information)
		{
            if (!Directory.Exists(LogPath))
            {
                Directory.CreateDirectory(LogPath);
            }
            string dateStr = DateTime.Now.ToString("yyyy-MM-dd").Replace("-","");
            string subPath = LogPath + "//" + dateStr;
            if (!Directory.Exists(subPath))
            {
                Directory.CreateDirectory(subPath);
            }
            if (logType == LogType.Data)
            {
                dateStr += "datalog.txt";
            }
            else if( (logType == LogType.Failure)||(logType == LogType.Information))
            {
                dateStr += "infolog.txt";
            }
            string[] logText = new string[] { DateTime.Now.ToString("HH:mm:ss:ff") + ": " + "["+logType.ToString() +"]"+ ": " + logContent +"\r\n"};
            lock (logLock)
            {
                File.AppendAllLines(subPath+"//"+dateStr, logText);
            }
		}
	}
}
